// pages/recharge/recharge.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    onePrice: {},
    maskShow: false,
    rangeMoney: '',
  },
  // 购买会员卡
  toCard: function (e) {
    wx.navigateTo({
      url: '/pages/card/card',
    })
  },
  // 获取一小时单价
  getOneprice: function () {
    var that = this;
    wx.request({
      url: config.getPrice,
      method: 'GET',
      data: {
      },
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token_type') + ' ' + wx.getStorageSync('access_token'),
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          onePrice: res.data.data,
        })
        console.log(res, "onePrice")
      }
    })
  },
  // 购买
  buyHour: function (e) {
    console.log(e.currentTarget.dataset.money)
    var that = this;
    wx.request({
      url: config.buyWallet,
      method: 'POST',
      data: {
        money: e.currentTarget.dataset.money,
      },
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token_type') + ' ' + wx.getStorageSync('access_token'),
      },
      success: function (res) {
        wx.requestPayment({
          'appId': res.data.data.appId,
          'timeStamp': res.data.data.timeStamp.toString(),
          'nonceStr': res.data.data.nonceStr,
          'package': res.data.data.package,
          'signType': res.data.data.signType,
          'paySign': res.data.data.paySign,
          'success': function (res) {
            console.log(res, '拉起支付')
          },
          'fail': function (res) { },
          'complete': function (res) { }
        })
      }
    })
  },
  // 显示任意金额弹窗
  changeMask: function () {
    this.setData({
      maskShow: true,
    })
  },
  // 拿到金额
  rangeMoney: function (e) {
    this.setData({
      rangeMoney: e.detail.value,
    })
    // console.log(this.data.phone,"phone")
  },
  // 提交金额并支付
  subMask: function () {
    this.setData({
      maskShow: false,
    })
    // console.log(this.data.userInfos, "this.data.userInfos")
    // console.log(this.data.phone,"phone")
    var that = this;
    if (this.data.rangeMoney != '') {
      wx.request({
        url: config.buyWallet,
        method: 'POST',
        data: {
          money:this.data.rangeMoney,
        },
        header: {
          'content-type': 'application/json',
          'Authorization': wx.getStorageSync('token_type') + ' ' + wx.getStorageSync('access_token'),
        },
        success: function (res) {
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp.toString(),
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              console.log(res, '拉起支付')
            },
            'fail': function (res) { },
            'complete': function (res) { }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getOneprice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})